#ip次数的题目

import random
with open("ips.txt","w+") as  fp:
    for i in range(100):

        p = random.randint(0,255)
        fp.write(f"172.25.254.{p}\n")


fr =open("ips.txt","r")
file = {}
fr.seek(0)
list1 = fr.readlines()
# print(list1)
for i in list1:

    if i not in file:
        file[f'{i}'] = 1
    elif i in file:
        file[f'{i}'] += 1

file=sorted(file.items(),key=lambda e:e[1],reverse=True)
for   e,(ip , times) in enumerate(file):
    if e in range(0,10):
        print(f"第{e}的IP为{ip}" ,f"次数是{times}")


#词频统计
import re
with open("Walden(1).txt", "r", encoding="utf-8") as fd:
    word_list = []     # 存放所有单词，全部小写，并去除,.!等后缀，并去除空格字符串
    word_dict = {}     # 保留{word: count}键值对
    for line in fd.readlines():
        for word in line.strip().split(" "):
            word_list.append(re.sub(r"[.|!|,]", "", word.lower()))
    word_sets = list(set(word_list))   # 确保唯一
    word_dict = {word: word_list.count(word) for word in word_sets if word}
print(*word_dict.items())


